package com.afd.ozblip.activity;

import java.util.HashMap;
import java.util.Map;

import com.afd.ozblip.net.HttpUtil;
import com.afd.ozblip.util.AsyncAction;
import com.afd.ozblip.util.AsyncLoadData;
import com.afd.ozblip.util.Constants;
import com.afd.ozblip.util.StringUtil;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class UserDetailActivity extends BaseActivity {

	/****************** 视图变量 **************************/
	private EditText userNameEt;
	private RadioGroup userSexRg;
	private RadioButton manRb;
	private RadioButton womanRb;
	private EditText userAgeEt;
	private EditText userCareerEt;
	private EditText userTelEt;
	private Button finishBtn;
	/****************** 普通变量 **************************/
	private String userNameStr;
	private String userSex = "";
	private String userAgeStr = null;
	private String userCareerStr = null;
	private String userTelStr = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_write_info);
		// 启动返回功能
		toBack();
		// 寻找视图
		findViews();
		// 设置监听
		setListeners();
		// 获取参数
		getBundle();
		// 设置视图
		setViews();
	}

	/**
	 * 寻找视图
	 */
	public void findViews() {
		this.userNameEt = (EditText) findViewById(R.id.et_user_info_name);
		this.userAgeEt = (EditText) findViewById(R.id.et_user_info_age);
		this.userCareerEt = (EditText) findViewById(R.id.et_user_info_career);
		this.userTelEt = (EditText) findViewById(R.id.et_user_info_tel);
		this.userSexRg = (RadioGroup) findViewById(R.id.rg_user_info_sex);
		this.manRb = (RadioButton) findViewById(R.id.rb_user_info_man);
		this.womanRb = (RadioButton) findViewById(R.id.rb_user_info_woman);
		this.finishBtn = (Button) findViewById(R.id.btn_user_info_ok);
	}

	/**
	 * 设置监听
	 */
	public void setListeners() {
		View.OnClickListener userDetailClick = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.btn_user_info_ok:
					userNameStr = userNameEt.getText().toString();
					userAgeStr = userAgeEt.getText().toString();
					userCareerStr = userCareerEt.getText().toString();
					userTelStr = userTelEt.getText().toString();
					if (verifyData()) {
						requestUpdateUserInfoData();
					}
					break;

				default:
					break;
				}
			}
		};
		this.finishBtn.setOnClickListener(userDetailClick);
		this.userSexRg
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						switch (checkedId) {
						case R.id.rb_user_info_man:
							userSex = "1";
							break;
						case R.id.rb_user_info_woman:
							userSex = "0";
							break;

						default:
							break;
						}
					}
				});
	}

	/**
	 * 获取传递的参数
	 */
	private void getBundle() {
		// Intent intent = getIntent();
	}

	private void setViews() {
		if (commonUserInfo != null) {
			String userRealName = commonUserInfo.getUserRealName();
			if (!StringUtil.isNullOrEmpty(userRealName)) {
				this.userNameEt.setText(userRealName);
			}
			String userSex = commonUserInfo.getUserSex();
			if ("1".equals(userSex)) {
				this.manRb.setChecked(true);
			} else if ("0".equals(userSex)) {
				this.womanRb.setChecked(true);
			}
			String userAge = commonUserInfo.getUserAge();
			if (!StringUtil.isNullOrEmpty(userAge)) {
				this.userAgeEt.setText(userAge);
			}
			String userCareer = commonUserInfo.getUserCareer();
			if (!StringUtil.isNullOrEmpty(userCareer)) {
				this.userCareerEt.setText(userCareer);
			}
			String userTel = commonUserInfo.getUserTelephone();
			if (!StringUtil.isNullOrEmpty(userTel)) {
				this.userTelEt.setText(userTel);
			}
		}
	}

	/**
	 * 验证数据
	 */
	private boolean verifyData() {
		if (StringUtil.isNullOrEmpty(userNameStr)) {
			showLongToast(this, "请填写姓名", true);
			return false;
		}
		return true;
	}

	/**
	 * 更新用户详情数据
	 */
	private void requestUpdateUserInfoData() {
		if (commonUserInfo == null) {
			return;
		}
		AsyncLoadData ald = new AsyncLoadData(this, "修改中...",
				new AsyncAction() {

					@Override
					public void doLoad() throws Exception {
						// 获取提交参数
						Map<String, Object> addReviewParamMap = new HashMap<String, Object>();
						addReviewParamMap.put("userId",
								commonUserInfo.getUserId());
						addReviewParamMap.put("realName", userNameStr);
						addReviewParamMap.put("userSex", userSex);
						addReviewParamMap.put("userAge", userAgeStr);
						addReviewParamMap.put("userCareer", userCareerStr);
						addReviewParamMap.put("userTel", userTelStr);

						// 发送请求
						byte[] bUserDetail = HttpUtil.requestByPost(
								UserDetailActivity.this,
								Constants.UPDATE_USER_API, addReviewParamMap);
						if (bUserDetail != null) {

						}
					}

					@Override
					public void loadEnd() throws Exception {
						showShortToast(UserDetailActivity.this, "温馨提示：修改个人资料成功");
						finish();
					}

				});
		ald.execute(0);

	}
}